<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('sales.debtorsmaster.debtorno')" type="string" v-model="baTable.form.items!.debtorno" prop="debtorno" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.debtorno') })" />
                    <FormItem :label="t('sales.debtorsmaster.name')" type="string" v-model="baTable.form.items!.name" prop="name" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.name') })" />
                    <FormItem :label="t('sales.debtorsmaster.gender')" type="radio" v-model="baTable.form.items!.gender" prop="gender" :data="{ content: { '1': t('sales.debtorsmaster.gender 1'), '0': t('sales.debtorsmaster.gender 0'), '2': t('sales.debtorsmaster.gender 2') } }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.gender') })" />
                    <FormItem :label="t('sales.debtorsmaster.address1')" type="string" v-model="baTable.form.items!.address1" prop="address1" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address1') })" />
                    <FormItem :label="t('sales.debtorsmaster.address2')" type="string" v-model="baTable.form.items!.address2" prop="address2" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address2') })" />
                    <FormItem :label="t('sales.debtorsmaster.address3')" type="string" v-model="baTable.form.items!.address3" prop="address3" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address3') })" />
                    <FormItem :label="t('sales.debtorsmaster.address4')" type="string" v-model="baTable.form.items!.address4" prop="address4" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address4') })" />
                    <FormItem :label="t('sales.debtorsmaster.address5')" type="string" v-model="baTable.form.items!.address5" prop="address5" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address5') })" />
                    <FormItem :label="t('sales.debtorsmaster.address6')" type="string" v-model="baTable.form.items!.address6" prop="address6" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.address6') })" />
                    <FormItem :label="t('sales.debtorsmaster.currcode')" type="remoteSelect" v-model="baTable.form.items!.currcode" prop="currcode" :input-attr="{ pk: 'cncerp_fm_currencies.currabrev', field: 'currency', 'remote-url': '/admin/fm.Currencies/index' }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.currcode') })" />
                    <FormItem :label="t('sales.debtorsmaster.salestype')" type="remoteSelect" v-model="baTable.form.items!.salestype" prop="salestype" :input-attr="{ pk: 'cncerp_sales_salestypes.typeabbrev', field: 'sales_type', 'remote-url': '/admin/sales.Salestypes/index' }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.salestype') })" />
                    <FormItem :label="t('sales.debtorsmaster.clientsince')" type="datetime" v-model="baTable.form.items!.clientsince" prop="clientsince" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.clientsince') })" />
                    <FormItem :label="t('sales.debtorsmaster.holdreason')" type="remoteSelect" v-model="baTable.form.items!.holdreason" prop="holdreason" :input-attr="{ pk: 'cncerp_sales_holdreasons.reasoncode', field: 'reasondescription', 'remote-url': '/admin/sales.Holdreasons/index' }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.holdreason') })" />
                    <FormItem :label="t('sales.debtorsmaster.paymentterms')" type="remoteSelect" v-model="baTable.form.items!.paymentterms" prop="paymentterms" :input-attr="{ pk: 'cncerp_sales_paymentterms.termsindicator', field: 'terms', 'remote-url': '/admin/sales.Paymentterms/index' }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.paymentterms') })" />
                    <FormItem :label="t('sales.debtorsmaster.discount')" type="number" prop="discount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.discount" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.discount') })" />
                    <FormItem :label="t('sales.debtorsmaster.pymtdiscount')" type="number" prop="pymtdiscount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.pymtdiscount" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.pymtdiscount') })" />
                    <FormItem :label="t('sales.debtorsmaster.lastpaid')" type="number" prop="lastpaid" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lastpaid" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.lastpaid') })" />
                    <FormItem :label="t('sales.debtorsmaster.lastpaiddate')" type="datetime" v-model="baTable.form.items!.lastpaiddate" prop="lastpaiddate" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.lastpaiddate') })" />
                    <FormItem :label="t('sales.debtorsmaster.creditlimit')" type="number" prop="creditlimit" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.creditlimit" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.creditlimit') })" />
                    <FormItem :label="t('sales.debtorsmaster.invaddrbranch')" type="number" prop="invaddrbranch" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.invaddrbranch" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.invaddrbranch') })" />
                    <FormItem :label="t('sales.debtorsmaster.discountcode')" type="string" v-model="baTable.form.items!.discountcode" prop="discountcode" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.discountcode') })" />
                    <FormItem :label="t('sales.debtorsmaster.ediinvoices')" type="number" prop="ediinvoices" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ediinvoices" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.ediinvoices') })" />
                    <FormItem :label="t('sales.debtorsmaster.ediorders')" type="number" prop="ediorders" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ediorders" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.ediorders') })" />
                    <FormItem :label="t('sales.debtorsmaster.edireference')" type="string" v-model="baTable.form.items!.edireference" prop="edireference" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.edireference') })" />
                    <FormItem :label="t('sales.debtorsmaster.editransport')" type="string" v-model="baTable.form.items!.editransport" prop="editransport" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.editransport') })" />
                    <FormItem :label="t('sales.debtorsmaster.ediaddress')" type="string" v-model="baTable.form.items!.ediaddress" prop="ediaddress" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.ediaddress') })" />
                    <FormItem :label="t('sales.debtorsmaster.ediserveruser')" type="string" v-model="baTable.form.items!.ediserveruser" prop="ediserveruser" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.ediserveruser') })" />
                    <FormItem :label="t('sales.debtorsmaster.ediserverpwd')" type="string" v-model="baTable.form.items!.ediserverpwd" prop="ediserverpwd" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.ediserverpwd') })" />
                    <FormItem :label="t('sales.debtorsmaster.taxref')" type="string" v-model="baTable.form.items!.taxref" prop="taxref" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.taxref') })" />
                    <FormItem :label="t('sales.debtorsmaster.customerpoline')" type="number" prop="customerpoline" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.customerpoline" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.customerpoline') })" />
                    <FormItem :label="t('sales.debtorsmaster.typeid')" type="remoteSelect" v-model="baTable.form.items!.typeid" prop="typeid" :input-attr="{ pk: 'cncerp_sales_debtortype.typeid', field: 'typename', 'remote-url': '/admin/sales.Debtortype/index' }" :placeholder="t('Please select field', { field: t('sales.debtorsmaster.typeid') })" />
                    <FormItem :label="t('sales.debtorsmaster.language_id')" type="string" v-model="baTable.form.items!.language_id" prop="language_id" :placeholder="t('Please input field', { field: t('sales.debtorsmaster.language_id') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    debtorno: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.debtorno') })],
    name: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.name') })],
    gender: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.gender') })],
    currcode: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.currcode') })],
    salestype: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.salestype') })],
    clientsince: [buildValidatorData({ name: 'date', title: t('sales.debtorsmaster.clientsince') })],
    holdreason: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.holdreason') })],
    paymentterms: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.paymentterms') })],
    lastpaid: [buildValidatorData({ name: 'number', title: t('sales.debtorsmaster.lastpaid') })],
    lastpaiddate: [buildValidatorData({ name: 'date', title: t('sales.debtorsmaster.lastpaiddate') })],
    creditlimit: [buildValidatorData({ name: 'number', title: t('sales.debtorsmaster.creditlimit') })],
    editransport: [buildValidatorData({ name: 'email', title: t('sales.debtorsmaster.editransport') })],
    typeid: [buildValidatorData({ name: 'required', title: t('sales.debtorsmaster.typeid') })],
})
</script>

<style scoped lang="scss"></style>
